package com.suoluo.zenadmin.controllers.mobile;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;

import org.springframework.web.servlet.ModelAndView;

import com.suoluo.zenadmin.common.UBB;
import com.suoluo.zenadmin.common.config.SystemConfigHolder;
import com.suoluo.zenadmin.service.dest.DestService;
import com.suoluo.zenadmin.service.dest.MobileService;
import com.suoluo.zenadmin.service.hotel.HotelService;
import com.suoluo.zenadmin.vo.country.ContinentVO;
import com.suoluo.zenadmin.vo.dest.DestVO;
import com.suoluo.zenadmin.vo.hotel.Hotel2VO;
import com.suoluo.zenadmin.vo.hotel.HotelResource2VO;

/**
 * 手机的酒店的action
 * 
 * @author xiongxu
 */
@Path("")
public class MobileHotelController {


	@Resource(name="hotelService")
	private HotelService hotelService;
	
	@Resource(name="destService")
	private DestService destService;
	
	@Resource(name = "mobileService")
	private MobileService mobileService;

	/**
	 * http://www.zentravel.cn/mobile/hotel.do?action=viewHotelHome
	 * @param inv
	 * @param action
	 * @return
	 */
	@Get("hotel.do")
	public String get(Invocation inv,@Param("action") String action){
		if ("viewHotelHome".equals(action)){
			return viewHotelHome(inv);
		}else if ("viewDestHotels".equals(action)){
			return viewDestHotels(inv);
		}else if ("viewHotel".equals(action)){
			return viewHotel(inv);
		}else if ("viewHotelResource".equals(action)){
			return viewHotelResource(inv);
		}else{
			return "@Not Found";
		}
	}

	/**
	 * 跳转到酒店精选首页
	 * 
	 * @param reqeust
	 * @param response
	 * @return
	 */
	private String viewHotelHome(Invocation inv) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("mobile/");
		List<ContinentVO> coninentTree = destService.findHotelNavigateTree();
		inv.addModel("coninentTree", coninentTree);
		return "hotel/hotelOverview";
	}

	/**
	 * 跳转到指定目的地的酒店列表页面
	 * 
	 * @param reqeust
	 * @param response
	 * @return
	 */
	private String viewDestHotels(Invocation inv) {
//		ModelAndView mv = new ModelAndView();
		
		Integer destId = Integer.valueOf(inv.getParameter("destId"));

		// 1.查询目的地信息
//		TravelDestinationPOJO dest = destService.findTravelDestinationById(destId);
		DestVO dest = destService.findSimplestDestById(destId);
		inv.addModel("dest", dest);

		// 2.查询目的地下的酒店信息
		List<Hotel2VO> hotels = hotelService.findHotelsWithoutContentByDestId(destId);
		inv.addModel("hotels", hotels);
		return "hotel/listDestHotels";
	}

	/**
	 * 跳转到酒店详细信息页面
	 * 
	 * @param reqeust
	 * @param response
	 * @return
	 */
	private String viewHotel(Invocation inv) {
		Integer hotelId = Integer.valueOf(inv.getParameter("hotelId"));
		
		//1.查询酒店信息
		Hotel2VO hotel = hotelService.findHotelById(hotelId);
		String hotelDesc = UBB.decode(hotel.getContent());
		hotel.setContent(hotelDesc);
		inv.addModel("hotel", hotel);

		// 2.查询该酒店的所在的目的地
		DestVO dest = destService.findSimplestDestById(hotel.getDestId());
		inv.addModel("dest", dest);

		//3. 酒店的资源分类
		int houseType = SystemConfigHolder.getHotelResourceHouse();
		List<HotelResource2VO> hourseList = new ArrayList<HotelResource2VO>();
		inv.addModel("hourse", hourseList);
		// 餐厅
		int restaurantType = SystemConfigHolder.getHotelResourceRestaurant();
		List<HotelResource2VO> restaurantList = new ArrayList<HotelResource2VO>();
		inv.addModel("restaurant", restaurantList);
		// 特色
		int special = SystemConfigHolder.getHotelResourceSpecial();
		List<HotelResource2VO> specialList = new ArrayList<HotelResource2VO>();
		inv.addModel("special", specialList);

		// 4.查询酒店资源信息
		List<HotelResource2VO> list = hotelService.findHotelResourcesWithoutContentByHotelId(hotelId);
		for (HotelResource2VO resource : list) {
			if (resource.getResourceType() == houseType) {
				hourseList.add(resource);
			} else if (resource.getResourceType() == restaurantType) {
				restaurantList.add(resource);
			} else if (resource.getResourceType() == special) {
				specialList.add(resource);
			}
		}
		return "hotel/hotelDetail";
	}

	/**
	 * 跳转到酒店指定资源类型详细信息页面
	 * 
	 * @param reqeust
	 * @param response
	 * @return
	 */
	private String viewHotelResource(Invocation inv) {
//		ModelAndView mv = new ModelAndView();
		// 获取资源Id
		int resourceId = Integer.parseInt(inv.getParameter("resourceId"));
		HotelResource2VO resource = hotelService.findSimplestHotelResourceById(resourceId);
		resource = hotelService.findHotelResourceAndItems(resource);
		inv.addModel("resource", resource);
		
		//1.查询酒店信息
		Hotel2VO hotel = hotelService.findHotelById(resource.getHotelId());
		inv.addModel("hotel", hotel);
		
		// 获取酒店特色资源,过滤当前资源
		List<HotelResource2VO> resources = hotelService.findHotelResourcesWithoutContentByHotelId(resource.getHotelId(), resource.getResourceType());
//		List<HotelResourcePOJO> moreResources = hotelService.findHotelResources(, );
		for (Iterator<HotelResource2VO> i = resources.iterator();i.hasNext();){
			HotelResource2VO r = i.next();
			if (r.getResourceId() == resourceId){
				i.remove();
				break;
			}
		}
		inv.addModel("moreResources", resources);
		
		return "hotel/hotelResource";
	}

}
